import datetime
from rest_framework import serializers

from news.models import NewsCategories, News, Comment, Collections, Authorfollow


class CategoriesSerializer(serializers.ModelSerializer):
    class Meta:
        model = NewsCategories
        fields=("id","category_name",'is_delete')



class NewslistSerializer(serializers.ModelSerializer):
    class Meta:
        model=News
        fields=("id","title","digest","create_time","user","image",'click_count')
        depth=1


class NewsDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = News
        fields = ("id","title","user","create_time","concent",'click_count')
        depth = 1



class NewscommentSerializer(serializers.ModelSerializer):
    class Meta:
        model=Comment
        fields=('content','count','news_id','user_id')
        depth = 1


class NewsCollectionSerializer(serializers.Serializer):
    # ACTION_CHOICES = (
    #     (1, "收藏"),
    #     (0, "取消收藏"),
    # )
    # news_id = serializers.IntegerField()
    # action = serializers.ChoiceField(choices=ACTION_CHOICES)
    #
    # def validate(self, attrs):
    #     news_id = attrs.get('news_id')
    #     try:
    #         news = News.objects.get(id=news_id)
    #     except News.DoesNotExist:
    #         raise serializers.ValidationError('新闻不存在')
    #     user = self.context['request'].user
    #     if not user.id:
    #         raise serializers.ValidationError('用户未登陆')
    #
    #     return attrs
    class Meta:
        model=Collections
        fields=('news_id','user_id')
        depth = 1


class NewsSubmitSerializer(serializers.ModelSerializer):

    class Meta:
        model = News
        fields = (
        'title', 'concent', 'tag')
        extra_kwargs={
            'create_time': {'null': True},
            'comment_conut': {'null': True},

        }

    def create(self, validated_data):

        validated_data["create_time"]= datetime.datetime.now()

        result = self.context['request'].data.get('category')
        cat=NewsCategories.objects.get(category_name=result)
        validated_data['category'] = cat

        user = self.context['request'].user
        validated_data['user'] = user

        validated_data['click_count'] = 0
        validated_data['comment_conut'] = 0

        digest = validated_data['concent'][3:50]
        validated_data['digest']=digest
        # print(validated_data)

        return super().create(validated_data)


class NewsfollowdSerializer(serializers.ModelSerializer):
    class Meta:
        model=Authorfollow
        fields=('author','user')
        depth = 1

    # def validate(self, attrs):
    #     # user=self.context['request'].user.id
    #     # attrs['user']=user
    #     author=attrs.get('author')
    #     user=attrs.get('user')
    #     user_queryset = Authorfollow.objects.filter(user=user)
    #     print(user_queryset)
    #     for use in user_queryset:
    #
    #         if use.author:
    #             raise serializers.ValidationError('已关注')
    #
    #     return attrs
class CommentSerializer(serializers.ModelSerializer):

    class Meta:
        model=Comment
        fields=('content',)
        depth = 1

    def create(self, validated_data):

        validated_data["create_time"] = datetime.datetime.now()

        user = self.context['request'].user.id
        validated_data['user_id_id'] = user
        print(validated_data)
        new = self.context['request'].data.get('news')

        validated_data['news_id_id'] = new
        validated_data['count'] = 0

        content = validated_data['content']
        validated_data['content'] = content
        return super().create(validated_data)

